package com.yxj04;

/*
* - InputStreamReader：是从字节流到字符流的桥梁
它读取字节，并使用指定的编码将其解码为字符
它使用的字符集可以由名称指定，也可以被明确指定，或者可以接受平台的默认字符集

- OutputStreamWriter：是从字符流到字节流的桥梁
是从字符流到字节流的桥梁，使用指定的编码将写入的字符编码为字节
它使用的字符集可以由名称指定，也可以被明确指定，或者可以接受平台的默认字符集*/

import java.io.*;

public class ConversionStreamDemo {
    public static void main(String[] args) throws IOException {
        //OutputStreamWriter(OutputStream out) 创建一个默认字符编码的OutputStreamWriter
        //OutputStreamWriter(OutputStream out,String charsetName) 创建一个使用命令字符编码的OutputStreamWriter
        //FileOutputStream fos = new FileOutputStream("D:\\Ccode\\jdata-structure\\ByteStream9\\osw.txt");
//        OutputStreamWriter osw = new OutputStreamWriter(fos);
        //OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\Ccode\\jdata-structure\\ByteStream9\\osw.txt"),"UTF-8");
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("D:\\Ccode\\jdata-structure\\ByteStream9\\osw.txt"),"GBK");
        osw.write("中国");
        osw.close();

        //InputStreamReader(InputStream in) 创建一个使用默认字符集的InputStreamReader
        //InputStreamReader(InputStream in,String charsetName) 创建一个使用命名字符集的InputStreamReader
        //InputStreamReader ist = new InputStreamReader(new FileInputStream("D:\\Ccode\\jdata-structure\\ByteStream9\\osw.txt"));
        InputStreamReader ist = new InputStreamReader(new FileInputStream("D:\\Ccode\\jdata-structure\\ByteStream9\\osw.txt"),"GBK");

        //一次读一个数据
        int ch;
        while((ch=ist.read()) != -1) {
            System.out.print((char)ch);
        }
        ist.close();

    }
}
